package com.ximu.prize.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ximu.common_utlis.jwt.JwtUtil;
import com.ximu.common_utlis.result.Result;
import com.ximu.prize.entity.Prize;
import com.ximu.prize.mapper.PrizeMapper;
import com.ximu.prize.vo.PrizeVo;
import com.ximu.user.entity.User;
import com.ximu.user.entity.UserVo;
import com.ximu.user.mapper.UserMapper;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ximu
 * @since 2023-03-20
 */
@RestController
@RequestMapping("/prize")
@Api(tags = "我的奖品接口")
public class PrizeController {

    @Autowired
    private PrizeMapper prizeMapper;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "查询用户实体奖品")
    @PostMapping("findAllPrize")
    public Result findAllCars(@RequestHeader("Authorization") String token){

        try {
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();

//            QueryWrapper<Prize> queryWrapper = new QueryWrapper();
//            queryWrapper.eq("user_id", Integer.parseInt(subject));

            List<PrizeVo> prizes = prizeMapper.findAllPrize(Integer.parseInt(subject));
            return Result.ok().data("prizes",prizes).message("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("查询失败，检查是否登录");
    }

    @ApiOperation(value = "查询未被抽取奖品---废弃")
    @PostMapping("findUsableCars")
    public Result findUsableCars(@RequestHeader("Authorization") String token){
        try {
            QueryWrapper<Prize> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", 0);

            List<Prize> prizes = prizeMapper.selectList(queryWrapper);
            return Result.ok().data("prizes",prizes).message("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("查询失败");
    }

    @ApiOperation(value = "抽到现金")
    @PostMapping("raffleCash")
    public Result raffleCash(@RequestHeader("Authorization") String token, BigDecimal cash){
        try {
            //获取id
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();
            //查询信息
            User updateUser = userMapper.selectById(subject);
            //金额
            BigDecimal cashs =updateUser.getBalance();
            //修改部分字段
            updateUser.setBalance(cashs.add(cash));

            int rs = userMapper.updateById(updateUser);
            if (rs > 0) {
                User rsuser = userMapper.selectById(subject);
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(rsuser, userVo);
                userVo.setPassword(null);
                return Result.ok().data("user", userVo).message("余额入账成功");
            } else {
                return Result.error().message("余额入账失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("余额入账失败");
    }



    @ApiOperation(value = "抽到积分")
    @PostMapping("raffleIntegral")
    public Result raffleIntegral(@RequestHeader("Authorization") String token, int integral){
        try {
            //获取id
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();
            //查询信息
            User updateUser = userMapper.selectById(subject);
            //积分
            int integrals =updateUser.getIntegral();
            //修改部分字段
            updateUser.setIntegral(integrals+integral);
            int rs = userMapper.updateById(updateUser);
            if (rs > 0) {
                User rsuser = userMapper.selectById(subject);
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(rsuser, userVo);
                userVo.setPassword(null);
                return Result.ok().data("user", userVo).message("积分入账成功");
            } else {
                return Result.error().message("积分入账失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("积分入账失败");
    }


    @ApiOperation(value = "抽到实物")
    @PostMapping("raffleGift")
    public Result raffleGift(@RequestHeader("Authorization") String token,@RequestBody Prize prize){
        try {
            //获取id
            Claims claims = JwtUtil.parseJWT(token);
            String subject = claims.getSubject();
            prize.setPrizeId(null);
            prize.setFailureTime(null);
            int rs = prizeMapper.insert(prize);
            if (rs > 0) {
                List<PrizeVo> prizes = prizeMapper.findAllPrize(Integer.parseInt(subject));
                return Result.ok().data("prizes",prizes).message("实物入账成功");
            } else {
                return Result.error().message("实物入账失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error().message("实物入账失败");
    }
}

